<template>
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form
            ref="form"
            label-width="90px"
            class="form"
            :model="form"
            :rules="rules"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy>
          <Info :user-info="initUserInfo" @getData="getData" />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" lazy>
          <Job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser } from '@/api/user'
import { sysUserPut } from '@/api/employees'
import { mapState, mapActions } from 'vuex'
// import Info from './components/Info.vue'
export default {
  components: {
    // 异步组件
    Info: () => import('./components/Info.vue'),
    Job: () => import('./components/Job.vue')
  },
  data() {
    return {
      id: this.$route.params.id,
      userInfo: {},
      initUserInfo: {},
      form: {
        username: '', //	string	非必须
        password: '' //	string	非必须
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        password: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须
      }
    }
  },
  computed: {
    ...mapState('user', {
      userInfoVuex: 'userInfo'
    })
  },
  created() {
    this.getData()
  },
  methods: {
    ...mapActions('user', ['getUserInfo']),
    // 获取用户信息
    async getData() {
      const { data } = await sysUser(this.id)
      this.initUserInfo = JSON.parse(JSON.stringify(data))
      this.userInfo = data
      this.form.username = data.username
      console.log(data)
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          // 修改用户信息
          await sysUserPut({ ...this.userInfo, ...this.form })
          // 刷新数据
          this.getData()
          this.$message.success('更新成功')
          // 如果是当前登陆帐号 更新vuex用户信息
          if (this.id === this.userInfoVuex.id) {
            // this.$store.dispatch('user/getUserInfo')
            this.getUserInfo()
          }
          console.log('验证成功')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.form {
  width: 500px;
  margin-left: 180px;
}
</style>
